package com.hc.contorl;

import com.hc.pojo.TbUser;
import com.hc.service.TbUserService;
import com.hc.service.impl.TbUserServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/userEdit")
public class UserEditController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String id=req.getParameter("id");

        String pagesize=req.getParameter("pageSize");
        String pageNum=req.getParameter("pageNum");
        String username=req.getParameter("userName");
        String password=req.getParameter("password");
        String email=req.getParameter("email");
        String phone=req.getParameter("phone");
        TbUserService service=new TbUserServiceImpl();
        TbUser user=service.findById(Long.parseLong(id));
        user.setUserName(username);
        user.setEmail(email);
        String oldpassword=user.getPassword();
        user=service.findById(Long.parseLong(id));
        if(password!=null&&(!password.equals(oldpassword))){
            password= DigestUtils.md5Hex(password);
            user.setPassword(password);
        }
        user.setPhone(phone);
//        System.out.println(user);
        int line=service.updateTbUer(user);
        if(line>0){
            req.setAttribute("message","修改成功！");
        }else{
            req.setAttribute("message","修改失败！");
        }
        req.setAttribute("reqPageSize",pagesize);
        req.setAttribute("reqPageNum",pageNum);
        req.getRequestDispatcher("/user_form.jsp").forward(req,resp);

    }
}
